﻿using MvvmCross.Core.ViewModels;
using Sidvall.AuthorizationManager.UI.Entities;
using System.Threading.Tasks;
using System.Windows.Input;

namespace Sidvall.AuthorizationManager.UI.ViewModels
{
    public class StartViewModel : Sidvall.UI.ViewModels.ViewModelBase
    {
        #region Public Members

        #region StartIsVisible

        private bool _StartIsVisible;
        public bool StartIsVisible
        {
            get
            {
                if (_PageType != null)
                    return (_PageType.Value == PageType.Start);
                return _StartIsVisible;
            }
            set
            {
                _StartIsVisible = value;
                RaisePropertyChanged("StartIsVisible");
            }
        }

        #endregion
        #region NewsIsVisible

        private bool _NewsIsVisible;
        public bool NewsIsVisible
        {
            get
            {
                if (_PageType != null)
                    return (_PageType.Value == PageType.News);
                return _NewsIsVisible;
            }
            set
            {
                _NewsIsVisible = value;
                RaisePropertyChanged("NewsIsVisible");
            }
        }

        #endregion
        #region UserConfiguration

        private UserConfiguration _UserConfiguration;
        public UserConfiguration UserConfiguration
        {
            get
            {
                return _UserConfiguration;
            }
            set
            {
                _UserConfiguration = value;
                RaisePropertyChanged("UserConfiguration");
            }
        }

        #endregion
        #region Version

        private string _Version;
        public string Version
        {
            get
            {
                return _Version;
            }
            set
            {
                _Version = value;
                RaisePropertyChanged("Version");
            }
        }

        #endregion
        #region WorkCaption

        private string _WorkCaption;
        public string WorkCaption
        {
            get
            {
                return _WorkCaption;
            }
            set
            {
                _WorkCaption = value;
                RaisePropertyChanged("WorkCaption");
            }
        }

        #endregion
        #region ProgressValue

        private double _ProgressValue;
        public double ProgressValue
        {
            get
            {
                return _ProgressValue;
            }
            set
            {
                _ProgressValue = value;
                RaisePropertyChanged("ProgressValue");
            }
        }

        #endregion
        #region NewsHtml

        private string _NewsHtml;
        public string NewsHtml
        {
            get
            {
                return _NewsHtml;
            }
            set
            {
                _NewsHtml = value;
                RaisePropertyChanged("NewsHtml");
            }
        }

        #endregion

        #region CloseNewsCommand

        public ICommand CloseNewsCommand
        {
            get
            {
                return new MvxCommand(CloseNews);
            }
        }

        #endregion
        #region CloseNews

        public void CloseNews()
        {
            ShowContent();
        }

        #endregion
        #region UnsubscribeToNewsCommand

        public ICommand UnsubscribeToNewsCommand
        {
            get
            {
                return new MvxCommand(async () => await UnsubscribeToNewsAsync());
            }
        }

        #endregion
        #region UnsubscribeToNewsAsync

        public async Task UnsubscribeToNewsAsync()
        {
            this.UserConfiguration.ShowNewsAtStartup = false;
            this.UserConfiguration = await Sidvall.AuthorizationManager.UI.SystemContext.Current.SaveUserConfigurationAsync(this.UserConfiguration, false);
            ShowContent();
        }

        #endregion

        #region ShowContent

        protected void ShowContent()
        {
            Sidvall.UI.SystemContext.Current.ApplicationManager.Status = Sidvall.UI.ApplicationStatus.Started;
            ShowViewModel<ToolViewModel>((object)null, false, "HeaderContent");
            ShowViewModel<NavigationViewModel>((object)null, false, "LeftContent");
            if (!Sidvall.AuthorizationManager.UI.SystemContext.Current.ServerManagers.IsValid)
            {
                ShowViewModel<DocumentationViewModel>((object)null, false, "CenterContent");
            }
            else
            {
                switch (this.UserConfiguration.StartPageId)
                {
                    case Sidvall.AuthorizationManager.Data.PageType.Manual:
                        ShowViewModel<DocumentationViewModel>((object)null, false, "CenterContent");
                        break;
                    case Sidvall.AuthorizationManager.Data.PageType.AuthorizationScopes:
                        ShowViewModel<AuthorizationScopeViewModel>((object)null, false, "CenterContent");
                        break;
                    case Sidvall.AuthorizationManager.Data.PageType.Servers:
                        ShowViewModel<ServerViewModel>((object)null, false, "CenterContent");
                        break;
                    case Sidvall.AuthorizationManager.Data.PageType.ResourceItems:
                        ShowViewModel<AuthorizationScopeResourceItemViewModel>((object)null, false, "CenterContent");
                        break;
                    default:
                        ShowViewModel<DashboardViewModel>((object)null, false, "CenterContent");
                        break;
                }
            }
        }

        #endregion

        #region StartAsync

        protected override async Task StartAsync()
        {
            System.Collections.Generic.List<System.Threading.Tasks.Task> tasks;

            tasks = new System.Collections.Generic.List<System.Threading.Tasks.Task>();
            _PageType = null;
            this.NewsIsVisible = false;
            this.StartIsVisible = true;
            this.ProgressValue = 0;
            this.WorkCaption = "Loading user configuration";
            tasks.Add(Sidvall.AuthorizationManager.UI.SystemContext.Current.LoadUserConfigurationAsync());
            tasks.Add(Sidvall.AuthorizationManager.SystemContext.Current.LoadVersionAsync());
            await System.Threading.Tasks.Task.WhenAll(tasks);
            this.UserConfiguration = Sidvall.AuthorizationManager.UI.SystemContext.Current.UserConfiguration;
            this.Version = Sidvall.AuthorizationManager.SystemContext.Current.Version;

            this.ProgressValue = 30;
            this.WorkCaption = "Logging on";
            await Sidvall.AuthorizationManager.UI.SystemContext.Current.LoadServerInfoAsync(true, false);
            this.UserConfiguration = Sidvall.AuthorizationManager.UI.SystemContext.Current.UserConfiguration;

            this.ProgressValue = 60;
            this.WorkCaption = "Loading configuration from servers";
            await Sidvall.AuthorizationManager.UI.SystemContext.Current.LoadServerManagersAsync();

            this.ProgressValue = 90;
            this.WorkCaption = "Checking version";
            if (this.Version != Sidvall.AuthorizationManager.Configuration.SettingsManager.CurrentVersion)
            {
                if (!this.UserConfiguration.ShowNewsAtStartup)
                {
                    this.UserConfiguration.ShowNewsAtStartup = true;
                    this.UserConfiguration = await Sidvall.AuthorizationManager.UI.SystemContext.Current.SaveUserConfigurationAsync(this.UserConfiguration, false);
                }
                this.Version = await Sidvall.AuthorizationManager.SystemContext.Current.SaveVersionAsync(Sidvall.AuthorizationManager.Configuration.SettingsManager.CurrentVersion);
            }

            this.ProgressValue = 100;
            if (this.UserConfiguration.ShowNewsAtStartup)
            {
                _LoadContent = false;
                this.NewsHtml = await Sidvall.SystemContext.Current.SystemStorageManager.ReadAllTextAsync(@"Data\News.htm");
                this.NewsIsVisible = true;
                this.StartIsVisible = false;
            }
            else
            {
                _LoadContent = true;
            }
        }

        #endregion
        #region OnAfterStart

        protected override void OnAfterStart()
        {
            if (_LoadContent)
                ShowContent();
        }

        #endregion

        #endregion
        #region Private Members

        private PageType? _PageType = PageType.News;
        private enum PageType
        {
            Start,
            News,
        }
        private bool _LoadContent;

        #endregion
    }
}
